<?php


namespace app\common\lib;


class Helper
{
    public static function debugLog($params, $pathFile = 'debug.log', $fileSize = 2) {
        clearstatcache();
        $path = dirname($pathFile);
        $realPath = $path == '.' ? '' : ROOT . 'runtime/' . $path;
        if ($realPath && !is_dir($realPath)) {
            mkdir($realPath, 0755, true);
        }
        $realFile = ROOT . 'runtime/' . $pathFile;
        $size = file_exists($realFile) ? @filesize($realFile) : 0;
        //标志是否附加文件.文件控制在1M大小
        $flag = $size < max(1, $fileSize) * 1024 * 1024;
        //简单数据
        is_scalar($params) or ($params = var_export($params, true));
        $prefix = date('Y-m-d H:i:s') . ' ';
        @file_put_contents($realFile, $prefix . $params . "\n", $flag ? FILE_APPEND : null);
    }

    public static function readLog($filePath)
    {
        $fh = fopen($filePath, 'r');
        $data = [];
        while (!feof($fh)){
            $row = fgets($fh);
            $arr = explode(' ', $row);
            //p($arr, basename($arr[4]));
            //$data[] = trim($arr[4]);
            $data[] = ['id' => $arr[2], 'img_src' => basename($arr[4])];
        }
        fclose($fh);
        return $data;
    }

    public static function readLog2($filePath)
    {
        $fh = fopen($filePath, 'r');
        $data = [];
        while (!feof($fh)){
            $row = fgets($fh);
            $arr = explode(' ', $row);
            $arr = explode('：', $arr[3]);
            $data[] = trim($arr[1]);
        }
        fclose($fh);
        return $data;
    }

    //判断远程文件
    function check_remote_file_exists($url)
    {
        $curl = curl_init($url);
        // 不取回数据
        curl_setopt($curl, CURLOPT_NOBODY, true);
        // 发送请求
        $result = curl_exec($curl);
        $found = false;
        // 如果请求没有发送失败
        if ($result !== false) {
            // 再检查http响应码是否为200
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($statusCode == 200) {
                $found = true;
            }
        }
        curl_close($curl);

        return $found;
    }

    /**
     * @param $data
     * @return array
     */
    public static function multiCurl($data)
    {
        $conn = array();
        // 创建批处理cURL句柄
        $mh = curl_multi_init();
        foreach ($data as $i => $item) {
            // 创建一对cURL资源
            $conn[$i] = curl_init();
            // 设置URL和相应的选项
            curl_setopt($conn[$i], CURLOPT_URL, $item['url']);
            // 不取回数据
            curl_setopt($conn[$i], CURLOPT_NOBODY, true);
            curl_setopt($conn[$i], CURLOPT_TIMEOUT, 10);
            curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT, 10);

            // 增加句柄
            curl_multi_add_handle($mh, $conn[$i]);
        }

        $active = null;

        // 防卡死写法：执行批处理句柄
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

        while ($active && $mrc == CURLM_OK) {
            if (curl_multi_select($mh) != -1) {
                do {
                    $mrc = curl_multi_exec($mh, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }

        $res = [];
        foreach ($data as $i => $row) {
            // 获取输出的文本流
            //$result = curl_multi_getcontent($conn[$i]);
            $info = curl_multi_info_read($conn[$i]);

            // 再检查http响应码是否为200
            $statusCode = curl_getinfo($conn[$i], CURLINFO_HTTP_CODE);
            $res[$i] = $statusCode;

            // 移除curl批处理句柄资源中的某个句柄资源
            curl_multi_remove_handle($mh, $conn[$i]);
            // 关闭cURL会话
            curl_close($conn[$i]);
        }
        // 关闭全部句柄
        curl_multi_close($mh);
        return $res;
    }
}